package com.github.wxiaoqi.security.biz.modules.withdrew.biz;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import com.github.wxiaoqi.security.auth.common.util.SnowFlake;
import com.github.wxiaoqi.security.biz.common.msg.BizResponseCode;
import com.github.wxiaoqi.security.biz.feign.CrmFeign;
import com.github.wxiaoqi.security.biz.feign.PayFeign;
import com.github.wxiaoqi.security.biz.modules.withdrew.entity.CmsWithdrewOrder;
import com.github.wxiaoqi.security.biz.modules.withdrew.mapper.CmsWithdrewOrderMapper;
import com.github.wxiaoqi.security.common.biz.BaseBiz;
import com.github.wxiaoqi.security.common.enums.CustomerTypeEnums;
import com.github.wxiaoqi.security.common.msg.TradeType;

import lombok.extern.slf4j.Slf4j;

/**
 * 提现表
 * @author he
 * @date 2018-09-09 13:37:13
 */
@Slf4j
@Service
public class CmsWithdrewOrderBiz extends BaseBiz<CmsWithdrewOrderMapper,CmsWithdrewOrder> {
	
	@Autowired
	private CmsWithdrewOrderMapper cmsWithdrewOrderMapper;
	@Autowired
	private CrmFeign crmFeign;
	@Autowired
	private PayFeign payFeign;
	
	/**
	 * 提现申请
	 * @param request
	 * @return
	 */
	public Map<String, Object> withdrewApply(@RequestBody Map<String, Object> params){
		log.info("提现申请请求.......{}",params);
		Map<String, Object> param = new HashMap<String, Object>();
		String orderId = String.valueOf(SnowFlake.getId());
		param.put("platformId",(String)params.get("platformId"));
		param.put("merchantId",(String)params.get("merchantId"));
		param.put("alipayNo", (String)params.get("alipayNo"));
		param.put("orderId", orderId);
		param.put("amount", (String)params.get("txAmt")); 
		param.put("payFee", "0"); 
		param.put("type", "01"); //个人提现
		Map<String, Object> payForAnotherApply = payFeign.payForAnotherApply(param);
		log.info("paycore代付申请结束..........{}",payForAnotherApply);
		if(!BizResponseCode.OK.getCode().equals(payForAnotherApply.get("code"))){
			Map<String, Object> resultMap = new HashMap<String, Object>();
			resultMap.put("code", BizResponseCode.WITHDREW_FAIL.getCode());
			resultMap.put("message", BizResponseCode.WITHDREW_FAIL.getMessage());
			return resultMap;
		}
		
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("platformId", (String)params.get("platformId"));
		map.put("merchantId", (String)params.get("merchantId"));
		map.put("personalId", (String)params.get("personalId"));
		map.put("orderNo", orderId);
		map.put("freezeAmt", (String)params.get("txAmt"));
		map.put("merchantType", CustomerTypeEnums.CUST_PERSONAL.getCode());//用户
		map.put("customerType", CustomerTypeEnums.CUST_PERSONAL.getCode());
		map.put("accountType", (String)params.get("accountType"));
		map.put("tradeType", TradeType.WITHDRAW.getCode());// 01：支付，02：退款，03：提现，04：充值，05：预收款，06：消费，07：结算，08：领取
		Map<String, Object> freezeMap = crmFeign.freeze(map);
		if(BizResponseCode.OK.getCode().equals(freezeMap.get("code"))){
			CmsWithdrewOrder cmsWithdrewOrder = new CmsWithdrewOrder();
			cmsWithdrewOrder.setPlatformId((String)params.get("platformId"));
			cmsWithdrewOrder.setMerchantId((String)params.get("merchantId"));
			cmsWithdrewOrder.setCustomerId((String)params.get("customerId"));
			cmsWithdrewOrder.setPersonalId((String)params.get("personalId"));
			cmsWithdrewOrder.setTxAmt(new BigDecimal((String)params.get("txAmt")));
			cmsWithdrewOrder.setWithdrewOrderNo(orderId);
			cmsWithdrewOrder.setCreateTime(new Date());
			cmsWithdrewOrder.setAlipayNo((String)params.get("alipayNo"));
			cmsWithdrewOrder.setTxStatus("02");//申请成功
			cmsWithdrewOrderMapper.insert(cmsWithdrewOrder);
		}
		log.info("提现申请响应.......{}",freezeMap);
		return freezeMap;
	}
}